package cn.seaboot.admin.api.mapping;

import cn.seaboot.admin.api.bean.ApiAction;
import cn.seaboot.commons.core.ArrayUtils;
import cn.seaboot.commons.core.Snowflake;
import cn.seaboot.commons.core.SnowflakeFactory;
import cn.seaboot.openapi.base.ControllerInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 扫描一个方法上的映射配置
 *
 * @author Mr.css
 * @version 2022-04-18 11:41
 */
@Component
public class ControllerScanner {
    private final Logger logger = LoggerFactory.getLogger(ControllerScanner.class);

    private final Snowflake snowflake = SnowflakeFactory.createSnowflake();

    /**
     * 扫描Controller
     *
     * @param projectId      项目ID
     * @param controllerInfo Controller 的 class
     * @return 一个Controller下的所有接口
     */
    public ApiAction scanControllerInfo(String projectId, ControllerInfo controllerInfo) {
        ApiAction ret = new ApiAction();

        ret.setId(this.snowflake.nextId());
        ret.setProjectId(projectId);

        ret.setControllerClass(controllerInfo.getTarget().getName());
        ret.setDescription(controllerInfo.getControllerDescription());

        RequestMapping mapping = controllerInfo.getRequestMapping();
        if (mapping != null) {
            String[] path = mapping.value();
            ret.setRequestMapping(ArrayUtils.join(path));
        }
        return ret;
    }
}
